#ifndef __D3D9RENDERER__
#define __D3D9RENDERER__

#include <windows.h>
#include <d3d9.h>
#include "hook.h"

#include "baserenderer.h"
#include "dxcommon.h"

class D3D9Renderer : public BaseRenderer
{
  HookData hEndScene;
  HookData hReset;

  class D3D9Texture : public BaseTexture
  {
  public:
    IDirect3DTexture9* pTexture;
    int width;
    int height;
    int size;
    D3D9Texture()
      : pTexture(NULL)
      , size(0)
    {}
    ~D3D9Texture()
    {
      COMRELEASE(pTexture);
    }
  };

  struct Vertex
  {
    float x, y, z;
    float rhw;
    D3DCOLOR color;
    float u, v;
  };

  IDirect3DDevice9* pCurDevice;
  IDirect3DStateBlock9* pOrigState;
  IDirect3DStateBlock9* pNewState;

  IDirect3DTexture9** pSourceTextures;
  int numSourceSizes;

  BaseTexture* newTexture()
  {
    return new D3D9Texture;
  }
  bool updateTexture(BaseTexture* tex, ImageHeader* image);
  void drawRect(BaseTexture* tex, int left, int top, int right, int bottom, unsigned long color);
  void setViewport(int left, int top, int right, int bottom);
  void reset();

  HWND getDeviceWindow(IDirect3DDevice9* pDevice);

  Vertex vertices[4];

  static D3D9Renderer* instance;
  static HRESULT __stdcall myEndScene(IDirect3DDevice9* pDevice);
  static HRESULT __stdcall myReset(IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* pParams);

  void render(IDirect3DDevice9* pDevice);
public:
  D3D9Renderer(HINSTANCE hInstance, HMODULE hD3D9, OverlayState const* pState);
  ~D3D9Renderer();

  void onDestroy(HWND hWnd);
};

#endif // __D3D9RENDERER__
